﻿using System.Xml.Linq;
using System;

using Sonic.Framework.Site;
using Sonic.Framework.Extensions;
using Enums = Sonic.Framework.Site.Enums;

using NUnit.Framework;

namespace Sonic.Framework.Tests.Extensions
{
    [TestFixture]
    public class XAttributeExtensionsTest
    {
        private XAttribute mockXAttributeWithString = new XAttribute(XName.Get("TestAttribute"), "TestValue");
        private XAttribute mockXAttributeWithBool = new XAttribute(XName.Get("TestAttribute"), false);
        private XAttribute mockXAttributeWithStringArray = new XAttribute(XName.Get("TestAttribute"), "TestValue1,TestValue2");
        private XAttribute mockXAttributeWithEnumStringValue = new XAttribute(XName.Get("TestAttribute"), "File");
        private XAttribute mockEmptyXAttribute = new XAttribute(XName.Get("TestAttribute"), "");

        /// <summary>
        ///A test for ValueOrDefault when expecting a string result
        ///</summary>
        [Test]
        public void ValueOrDefaultStringTest()
        {
            XAttribute xattribute = mockXAttributeWithString;

            string defaultValue = "DefaultValue";
            string expected = "TestValue";

            string actual = XAttributeExtensions.ValueOrDefault(xattribute, defaultValue);

            Assert.AreEqual(expected, actual);
        }


        /// <summary>
        ///A test for ValueOrDefault when expecting a bool result
        ///</summary>
        [Test]
        public void ValueOrDefaultBoolTest()
        {
            XAttribute xattribute = mockXAttributeWithBool;

            bool defaultValue = true;
            bool expected = false;

            bool actual = XAttributeExtensions.ValueOrDefault(xattribute, defaultValue);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ValueOrDefault when expecting a bool result
        ///</summary>
        [Test]
        public void ValueOrDefaultStringArrayTest()
        {
            XAttribute xattribute = mockXAttributeWithStringArray;

            string[] defaultValue = new string[] { "DefaultValue1", "DefaultValue2" };
            string[] expected = new string[] { "TestValue1", "TestValue2" };

            string[] actual = XAttributeExtensions.ValueOrDefault(xattribute, defaultValue, ',');

            Assert.IsTrue(actual.Length == expected.Length);

            for (int i = 0; i < actual.Length; i++)
            {
                Assert.AreEqual(actual[i], expected[i]);
            }
        }

        /// <summary>
        ///A test for ValueOrDefault when expecting a Enum result
        ///</summary>
        [Test]
        public void ValueOrDefaultEnumStringTest()
        {
            XAttribute xattribute = mockXAttributeWithEnumStringValue;

            Enum defaultValue = Enums.SitePathScope.Directory;
            Enum expected = Enums.SitePathScope.File;

            Enum actual = XAttributeExtensions.ValueOrDefault(xattribute, defaultValue, typeof(Enums.SitePathScope));

            Assert.AreEqual(expected, actual);
        }
    }
}
